#include "downloadapplication.h"
#include "ui_downloadapplication.h"
#include "downloadobject.h"
#include "downloadlistwidgets.h"
#include "downloadnewfiledialog.h"
#include "downloadbgskindialog.h"
#include "downloadconfigmanager.h"

DownloadApplication::DownloadApplication(QWidget *parent) :
    DownloadMoveWidgetAbstract(parent),
    ui(new Ui::DownloadApplication), m_backgroundSkinDialog(NULL)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);

    setAttribute(Qt::WA_TranslucentBackground, true);
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),4,4);
    setMask(bmp);

    m_listWidget = new DownloadListWidgets(this);
    initWindowSurface();

    ui->stackedWidget->addWidget(m_listWidget);
    ui->stackedWidget->setCurrentIndex(1);

    readXMLConfigFromText();
}

DownloadApplication::~DownloadApplication()
{
    writeXMLConfigFromText();
    delete m_backgroundSkinDialog;
    delete m_listWidget;
    delete ui;
}

void DownloadApplication::readXMLConfigFromText()
{
    DownloadConfigManager xml(this);
    if(!xml.initXMLConfig())//open download file
        return;
    m_currentBgSkin = xml.readBackgroundTheme();
    //////////////////////////////////////////////////////////////
    //Set the current alpha value
    m_alpha = xml.readBackgroundTransparent().toInt();
    downloadBgTransparentChanged(m_alpha);
    //////////////////////////////////////////////////////////////
}

void DownloadApplication::writeXMLConfigFromText()
{
    DownloadConfigManager xml(this);
    if(!xml.initXMLConfig())//open download file
        return;
    QSettings setting;
    setting.setValue("BGTHEMECHOICED", m_currentBgSkin);
    setting.setValue("BGTRANSPARENTCHOICED", m_alpha);

    xml.writeXMLConfig();
}

void DownloadApplication::closeEvent(QCloseEvent *event)
{
    event->accept();
//    if(systemTray->isVisible())
//    {
//       this->hide();
//       systemTray->showMessage(tr("Prompt"),tr("QMusicPlayer will run in the background"));
//       event->ignore();
//    }
//    else
//    {
//       event->accept();
//    }
}

void DownloadApplication::initWindowSurface()
{
    creatSystemTrayIcon();
    setMenuBarButton();
    setButtonTips();
    setButtonCursor();

    connect(ui->windowMinimization,SIGNAL(clicked()),this,SLOT(showMinimized()));
    connect(ui->windowClose,SIGNAL(clicked()),this,SLOT(close()));
    connect(ui->windowNewDownload,SIGNAL(clicked()),this,SLOT(downloadAddNewFile()));
    connect(ui->windowStart,SIGNAL(clicked()),m_listWidget,SLOT(start()));
    connect(ui->windowStop,SIGNAL(clicked()),m_listWidget,SLOT(pause()));
    connect(ui->windowDelete,SIGNAL(clicked()),m_listWidget,SLOT(deleteItemFromList()));
    connect(ui->windowChangeSkin,SIGNAL(clicked()),SLOT(downloadShowBgSkin()));
}

void DownloadApplication::setMenuBarButton()
{
    QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    ui->windowMinimization->setIcon(QIcon(minPix.scaled(25,25)));
    ui->windowClose->setIcon(QIcon(QString::fromUtf8(":/image/windowClose")));
    ui->windowChangeSkin->setIcon(QIcon(QString::fromUtf8(":/image/windowSkin")));
    ui->windowNewDownload->setIcon(QIcon(QString::fromUtf8(":/image/newFile")));
    ui->windowStart->setIcon(QIcon(QString::fromUtf8(":/image/start")));
    ui->windowStop->setIcon(QIcon(QString::fromUtf8(":/image/pause")));
    ui->windowDelete->setIcon(QIcon(QString::fromUtf8(":/image/delete")));
    ui->windowDownloadOrder->setIcon(QIcon(QString::fromUtf8(":/image/order")));
    ui->windowSettings->setIcon(QIcon(QString::fromUtf8(":/image/settings")));
    ui->windowTools->setIcon(QIcon(QString::fromUtf8(":/image/tools")));
    ui->windowUploadLabel->setPixmap(QPixmap(QString::fromUtf8(":/image/upload")));
    ui->windowDownloadloadLabel->setPixmap(QPixmap(QString::fromUtf8(":/image/download")));

    ui->windowClose->setIconSize(QSize(20,20));
    ui->windowChangeSkin->setIconSize(QSize(30,30));
    ui->windowNewDownload->setIconSize(QSize(45,45));
    ui->windowStart->setIconSize(QSize(28,28));
    ui->windowStop->setIconSize(QSize(28,28));
    ui->windowDelete->setIconSize(QSize(24,24));
    ui->windowDownloadOrder->setIconSize(QSize(28,28));
    ui->windowSettings->setIconSize(QSize(28,28));
    ui->windowTools->setIconSize(QSize(36,36));

    ui->windowMinimization->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowClose->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowChangeSkin->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowNewDownload->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowStart->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowStop->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowDelete->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowDownloadOrder->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowSettings->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowTools->setStyleSheet(DownloadObject::QToolButtonStyle2);
    ui->windowDSButton->setStyleSheet(DownloadObject::QPushButtonStyle2);
    ui->windowDPButton->setStyleSheet(DownloadObject::QPushButtonStyle2);
}

void DownloadApplication::setButtonCursor()
{
    ui->windowMinimization->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowClose->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowChangeSkin->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowNewDownload->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowStart->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowStop->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowDelete->setCursor(QCursor(Qt::PointingHandCursor));
    ui->windowDownloadOrder->setCursor(QCursor(Qt::PointingHandCursor));
}

void DownloadApplication::setButtonTips()
{
    ui->windowMinimization->setToolTip(tr("Minimization"));
    ui->windowClose->setToolTip(tr("Close"));
    ui->windowChangeSkin->setToolTip(tr("changeskin"));
}

void DownloadApplication::creatSystemTrayIcon()
{
    m_systemTray = new QSystemTrayIcon(this);
    m_systemTray->setIcon(QIcon(QString::fromUtf8(":/image/windowIcon")));
    m_systemTray->setToolTip(tr("QDownloadApplication"));

//    creatMenuActions();
//    creatSystemTrayMenu();

//    systemTray->setContextMenu(systemTrayMenu);

    m_systemTray->show();
    connect(m_systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,
                     SLOT(windowIconActivated(QSystemTrayIcon::ActivationReason)));
}

void DownloadApplication::windowIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
      case QSystemTrayIcon::DoubleClick:
          break;
      case QSystemTrayIcon::Trigger:
          if(!this->isVisible())
          {
              this->show();
              this->activateWindow();
          }
          break;
      default:
          break;
    }
}

void DownloadApplication::downloadAddNewFile()
{
    DownloadNewFileDialog fileDialog;
    connect(&fileDialog,SIGNAL(setDownloadPath(QStringList)),
            m_listWidget, SLOT(addItemToList(QStringList)));
    fileDialog.exec();
}

void DownloadApplication::downloadStateChanged(bool& state)
{
    ui->windowStart->setEnabled(state);
    ui->windowStop->setEnabled(!state);
}

void DownloadApplication::downloadShowBgSkin()
{
    if(!m_backgroundSkinDialog)
        m_backgroundSkinDialog = new DownloadBgSkinDialog(this);
    m_backgroundSkinDialog->setCurrentBgTheme(m_currentBgSkin, m_alpha);
    m_backgroundSkinDialog->exec();
}

void DownloadApplication::downloadBgTransparentChanged(int index)
{
    m_alpha = index;//save the alpha
    //Here set the picture transparency
    QPixmap origin(THEME_DOWNLOAD + m_currentBgSkin + JPG_FILE);
    QPixmap afterDeal(size());
    afterDeal.fill(Qt::transparent);
    QPainter paint(&afterDeal);
    paint.fillRect(0,0,afterDeal.width(),afterDeal.height(),QColor(255,255,255,2.55*index));
    paint.setCompositionMode(QPainter::CompositionMode_SourceIn);
    paint.drawPixmap(0,0,QPixmap::fromImage(origin.scaled(size()).toImage()));
    paint.end();
    ui->background->setPixmap(afterDeal);
}

void DownloadApplication::downloadBackgroundSkinChanged(const QString &filename)
{
    m_currentBgSkin = filename;
    downloadBgTransparentChanged(m_alpha);
}
